// pages/my/stockManage/stockManage.js
var app = getApp()
Page({
  data: {
    topCurrentId:'all',
    page:2,
    leftCurrentId:'',
    parent_id:0,
    topNav:'',
    selectIndex: 0,
    isUpload:true,
    goodsList: [],
    leftBtn:[],
    topBtn:[],
    backBox:[],
    upDataList:[],
    upDataTime:'',
    agentId:'',
    goodsType:'',
    isMgAdd:false,
    isStock:false,
    isChangePrice:false,
    ifJoin:true,
    trash:false,
    noData:false,
    stockType:'',
    topCurrentIndex:"0",
    isSelf:false,
    hasOld:false,
    waitJoin:false,
    upData:false
  },
  onLoad: function(options) {
      if (options.type==0) {          //我的库存
        this.setData({
          isUpload:false,
          ifJoin:true,
          isStock:true,
          isMgAdd:true,
          stockType:options.type
        })
        wx.setNavigationBarTitle({
          title: '库存数量'
        })
      }else if (options.type==1) {    //我的商品
        this.setData({
          ifJoin:false,
          trash:true,
          stockType:options.type,
          isStock:true
        })
        wx.setNavigationBarTitle({
          title: '商品目录'
        })
      }else{                          //修改价格
        this.setData({
          isUpload:false,
          ifJoin:false,
          isStock:true,
          isChangePrice:true,
          stockType:options.type
        })
        wx.setNavigationBarTitle({
          title: '库存与定价'
        })
      }
      app.getMemInfo().then(res=>{
        console.log(res.data.agentId)
        let agentId = res.data.agentId
        this.setData({
          agentId
        })
        this.getDistribution()
        this.getUpdataList(agentId)
        // this.getFirstTypeList(res.data.agentId)
        // this.getGoodsTypeList(res.data.agentId,0)
        // this.getProductType(agentId)
      })
    },
    onShow(){
      app.getMemInfo().then(res=>{
        console.log(res.data.agentId)
        let agentId = res.data.agentId
        this.setData({
          agentId,
          selectIndex:0
        })
        this.getDistribution()
        this.getUpdataList(agentId)
        // this.getFirstTypeList(res.data.agentId)
        // this.getGoodsTypeList(res.data.agentId,0)
        // this.getProductType(agentId)
      })
    },
    //商品种类选择事件
    bindLeftBtn(e){
      let type = e.currentTarget.dataset.type
      this.setData({
        leftCurrentId:e.currentTarget.dataset.id,
        goodsType:type
      })
      this.getGoodsList('1')
    },
    //获取一级分类
    getFirstTypeList(agentId){
      const that = this
      let topBtn = this.data.topBtn
      let data = {
        agentId,
        requestType:'client'
      }
      app.request('wx_app/mall/product/type/list',data,'GET').then(res=>{
        console.log(res)
        res.data.product_types.forEach(item=>{
          if (item.parent_id==0) {
            topBtn.push({name:item.name,id:item.id})
          }
        })
        that.setData({
          topBtn,
          page:2
        })
      })
    },
    chooseAll(){
      this.setData({
        topCurrentId:'all',
        parent_id:0,
        leftCurrentId:0
      })
      this.getGoodsTypeList(this.data.agentId,0)
    },
    chooseFirstType(e){
      let id = e.currentTarget.dataset.index
      let type = e.currentTarget.dataset.type
      this.setData({
        topCurrentId:id,
        parent_id:type,
        leftCurrentId:0
      })

      this.getGoodsTypeList(this.data.agentId,type)
    },
  //获取商品类型列表
  getGoodsTypeList(agentId,type){
    const that = this
    let leftBtn = []
    let backBox = []
    let goodsType=''
    let data = {
      agentId,
      requestType:'client'
    }
    console.log(type)
    app.request('wx_app/mall/product/type/list',data,'GET').then(res=>{
      console.log(res)
      res.data.product_types.forEach(item=>{
        if (type==item.parent_id&&type!=0) {
          leftBtn.push({name:item.name,id:item.id})
        }else if (type==0) { //全部
          if (item.parent_id!=0) {
            leftBtn.push({name:item.name,id:item.id})
          }
        }
        if (type==item.id) { 
          backBox.push({name:item.name,id:item.id})
        }
      })
      that.setData({
        leftBtn,
        backBox
      })
      if (that.data.leftBtn==''&&that.data.topCurrentId=='all') {
        res.data.product_types.forEach(item=>{
          leftBtn.push({name:item.name,id:item.id})
        })
        that.setData({
          leftBtn
        })
      }
      if (that.data.leftBtn=='') {
        that.setData({
          leftBtn:that.data.backBox
        })
      }
      goodsType = that.data.leftBtn[0].id
      that.setData({
        goodsType,
        page:2
      })
      that.getGoodsList('1')
    })
  },
  //获取商品列表
  getGoodsList(page){
    wx.showLoading({
      title:'正在加载'
    })
    this.setData({
      goodsList:[]
    })
    var that = this
    var data = {
      agentId:this.data.agentId,
      type:this.data.goodsType,
      pageNum:page,
      searchKey:''
    }
    console.log(data)
    let goodsList = []
    app.request('wx_app/agent/stock/product/list',data,'GET').then(res=>{
      console.log(res)
      if (res.code==1) {      
        res.data.pageContent.forEach(item=>{
          goodsList.push({
            name:item.product_name,
            price:item.price,
            stock:item.stock,
            url:item.main_img_url,
            id:item.id,
            secondTypeId:item.product_type,
            color:item.tagColor,
            companyName:item.productTag
          })
          that.setData({
            goodsList
          })
        })
        wx.hideLoading()
        if (that.data.goodsList.length==0) {
          that.setData({
            noData:true
          })
        }else{
          that.setData({
            noData:false
          })
        }
      }else{
        that.setData({
          noData:true
        })
        wx.hideLoading()
        app.errorMsg(res.message)
      }
    })
  },
  bindMore(){
    wx.showLoading({
      title:'正在加载'
    })
    let page = this.data.page
    var that = this
    var data = {
      agentId:this.data.agentId,
      type:this.data.goodsType,
      pageNum:page,
      searchKey:''
    }
    let goodsList = this.data.goodsList
    app.request('wx_app/agent/stock/product/list',data,'GET').then(res=>{
      console.log(res)
      if (res.code==1) {      
        res.data.pageContent.forEach(item=>{
          goodsList.push({
            name:item.product_name,
            price:item.price,
            stock:item.stock,
            url:item.main_img_url,
            id:item.id,
            secondTypeId:item.product_type,
            color:item.tagColor,
            companyName:item.productTag
          })
          that.setData({
            goodsList
          })
        })
        wx.hideLoading()
      }else{
        wx.hideLoading()
        app.errorMsg(res.message)
      }
    })
    page++
    this.setData({
      page:page
    })
  },
  // comeStockTap: function(e) {
  //   console.log(e.currentTarget.dataset.id)
  //   wx.navigateTo({
  //     url: '../stockGoodsSet/stockGoodsSet?id='+e.currentTarget.dataset.id+'&type='+e.currentTarget.dataset.type
  //   })
  // },
  addnewGoodsTap: function() {
    wx.navigateTo({
      url: '../addGoods/addGoods',
    })
  },
  bindStockGoodSet(e){
    let id = e.currentTarget.dataset.id
    let type = e.currentTarget.dataset.type
    let price = e.currentTarget.dataset.price
    let stockType = e.currentTarget.dataset.stockType
    let secondId = e.currentTarget.dataset.secondId
    console.log(secondId)
    wx.navigateTo({
      url: '../stockGoodsSet/stockGoodsSet?id='+id+'&type='+type+'&price='+price+'&stockType='+stockType+'&second='+secondId
    })
  },
  addUnderLineGoods(e){
    let id = e.currentTarget.dataset.id
    let type = e.currentTarget.dataset.type
    let stockType = e.currentTarget.dataset.stockType
    let secondId = e.currentTarget.dataset.secondId
    console.log(e.currentTarget.dataset.stockType)
    wx.navigateTo({
      url:'../stockGoodsSet/stockGoodsSet?id='+id+'&type='+type+'&stockType='+stockType+'&secondId='+secondId
    })
  },
  selecttypeTap: function(event) {
    var dataSet = event.currentTarget.dataset;
    let type = dataSet.type
    var selectIndex = dataSet.index;
    var categoryId = dataSet.categoryId;//查询不同分类下的数据
    this.setData({
      selectIndex: selectIndex,
      goodsType:type,
      page:2
    })
    if(this.data.stockType==0){
      console.log(type)
    }
    this.getGoodsList('1')
  },
  jumpAddGoods(){
    wx.navigateTo({
      url:'../addCustomGoods/addCustomGoods'
    })
  },
  bindTopNav(e){
    console.log(e.currentTarget.dataset.index)
    let topNav = e.currentTarget.dataset.index
    this.setData({
      topNav
    })
    if (topNav==0) {
      this.setData({
        isUpload:true,
        isMgAdd:false,
        isStock:false,
        isChangePrice:false
      })
    }else{
      this.setData({
        isUpload:false,
        isMgAdd:false,
        isStock:true,
        isChangePrice:true
      })
    }
  },
  fromLibrary(){
    wx.navigateTo({
      url:'../library/library'
    })
  },
  addStock(){
    wx.navigateTo({
      url:'../addStock/addStock?type=0'
    })
  },
  addOldStock(){
    wx.navigateTo({
      url:'../addStock/addStock?type=1'
    })
  },
  bindDelete(e){
    let data = {
      skuId:e.currentTarget.dataset.id
    }
    var that = this
    wx.showModal({
      title:'删除商品',
      content:'删除商品后，对应库存信息也被删除！',
      confirmColor:'#539FFF',
      success(res){
        if (res.confirm) {
          console.log('用户点击确定')
          app.request('wx_app/agent/stock/del',data,'GET').then(res=>{
            console.log(res)
            if(res.code==1){
              that.getGoodsList('1')
            }else{
              app.errorMsg(res.message)
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
    this.setData({
      page:2
    })
    console.log(data)
  },
  bindSearch(){
    wx.navigateTo({
      url:'stockSearch?type='+this.data.stockType
    })
  },
  //获取配送商品
  getDistribution(e){
    if (e) {
      this.setData({
        topCurrentIndex:e.currentTarget.dataset.index,
        isSelf:false,
        waitJoin:false,
        hasOld:false
      })
    }
    let that = this
    let distribution = this.data.distribution
    let leftBtn = []
    let goodsType=''
    let data = {
      agentId:this.data.agentId,
      requestType:'client'
    }
    console.log(data)
    app.request('wx_app/mall/product/type/list',data,'GET').then(res=>{
      console.log(res)
      res.data.product_types.forEach(item=>{
        if (item.parent_id==0) {
            if (item.id!=1) {
              leftBtn.push({name:item.name,id:item.id})
            }
        }
      })
      that.setData({
        leftBtn
      })
      goodsType = that.data.leftBtn[0].id
      that.setData({
        goodsType,
        page:2
      })
      that.getGoodsList('1')
    })
  },
  //获取自营商品
  getSelfSupport(e){
    if(this.data.stockType==0){
      this.setData({
        hasOld:true
      })
    }else{
      this.setData({
        hasOld:false
      })
    }
    this.setData({
      topCurrentIndex:e.currentTarget.dataset.index,
      isSelf:true,
      waitJoin:true,
      goodsList:[],
      selectIndex:0
    })
    let that = this
    let distribution = this.data.distribution
    let leftBtn = []
    let goodsType=''
    let data = {
      agentId:this.data.agentId,
      requestType:'client'
    }
    console.log(data)
    app.request('wx_app/mall/product/type/list',data,'GET').then(res=>{
      console.log(res)
      res.data.product_types.forEach(item=>{
        if (item.parent_id==0) {
          if (item.id==1||item.id==98) {
            leftBtn.unshift({name:item.name,id:item.id})
          }
        }
      })
      that.setData({
        leftBtn
      })
      goodsType = that.data.leftBtn[0].id
      that.setData({
        goodsType,
        page:2
      })
      that.getGoodsList('1')
    })
  },
  //更新商品列表
  getUpdataList(agentId){
    let data = {
      agentId
    }
    app.request('wx_app/sku/update/list',data,'GET').then(res=>{
      console.log(res)
      if (res.code==1) {
        let upDataList = []
        let upDataTime = 0
        res.data.forEach(item=>{
          upDataList.push(item)
        })
        this.setData({
          upDataList,
          upDataTime:res.data.length
        })
      }else{
        app.errorMsg(res.message)
      }
    })
  },
  closeUpData(){
    this.setData({
      upData:false
    })
  },
  openUpData(){
    this.setData({
      upData:true
    })
  },
  upDataOne(e){
    let be = e.currentTarget.dataset.be
    let af = e.currentTarget.dataset.af
    let data = {
      updateBeforeSkuIds:be,
      updateAfterSkuAllIds:af
    }
    console.log(data)
    app.request('wx_app/sku/update',data,'GET').then(res=>{
      console.log(res)
      if (res.code==1) {
        this.getUpdataList(this.data.agentId)
        wx.showToast({
          title:'更新成功'
        })
      }else{
        app.errorMsg(res.message)
      }
    })

  },
  upDataAll(){
    let upDataList = this.data.upDataList
    console.log(upDataList)
    let be = '',af = ''
    upDataList.forEach(item=>{
      be+=item.before_update.skuId+','
      af+=item.after_update.id+','
    })
    let data = {
      updateBeforeSkuIds:be,
      updateAfterSkuAllIds:af
    }
    app.request('wx_app/sku/update',data,'GET').then(res=>{
      console.log(res)
      if (res.code==1) {
        this.getUpdataList(this.data.agentId)
        wx.showToast({
          title:'更新成功'
        })
        this.setData({
          upData:false
        })
      }else{
        app.errorMsg(res.message)
      }
    })
  }
})